Skip to main content

Loops

1. while loop

The while loop executes a set of statements as long as a condition is true.

while condition:
# code to be executed

Example: The variable count starts at 0, then is 1, then 2, then 3, then 4. When count is 5, the condition count < 5 is no longer True, so the loop stops. The loop is executed 5 times.

count = 0
while count < 5:
print(count)
count += 1

Using a while loop to print all multiples of 10 from 10 to 90 in order (including 10 and 90).

count = 10
while count < 91:
print(count)
count += 10

2. for loop

The for loop iterates over a sequence (like a list, tuple, string, or range) or other iterable objects.

for item in iterable:
# code to be executed
Example:
fruits = ["apple", "banana", "cherry"]
for fruit in fruits:
print(fruit)

# Using range
for i in range(5):
print(i)

range()

The for loop is often used with range() function, which generates a sequence of numbers.

  1. The range() function must accept at least one parameter, which indicates the ending number.
  • The function generates a sequence of numbers from 0 to that ending number. The ending number is not included in the sequence.
for i in range(3):
print(i) # Output: 0, 1, 2
  1. The range() function can also accept two parameters.
  • The first parameter to the range() function is the starting number.
  • The second parameter is the ending number. The ending number is not included in the sequence.
for i in range(2, 5):
print(i) # Output: 2, 3, 4
  1. The range function can also accept three parameters.
  • The first parameter to the range() function is the starting number.
  • The second parameter is the ending number. The ending number is not included in the sequence.
  • The third parameter is the step number, by default the step is 1.
for i in range(0, 10, 2):
print(i) # Output: 0, 2, 4, 6, 8

When passing a negative number as the third argument (step number) to the the range() function, we can create a reverse for loop.

for i in range(10, 0, -1):
print(i) # Output: 10, 9, 8, 7, 6, 5, 4, 3, 2, 1

You can also use the function reversed() to reverse a sequence.

  • for i in range(10): will iterate from 0 to 9.
  • for i in reversed(range(10)): will iterate from 9 to 0.

3. Nested Loops

A loop that are placed inside another loop is a called a nested loop.

  • In Python, loops do not create their own scope. When using nested loops, we must use different variable names for the nested loops.
  • The inner loop must be indented to show that it is inside the outer loop.
Example:
  • The outer loop will run 3 times total.
  • The inner loop will run 3 times, for each iteration of the outer loop.
  • This will result in the print statement being executed 9 times.
  • The inner loop will run to completion before the outer loop continues.
for i in range(1, 4):
for j in range(1, 4):
print(i, j)
# Output: 
1 1
1 2
1 3
2 1
2 2
2 3
3 1
3 2
3 3

4. Control Flow

Python provides control statements to alter the execution of loops.

  • break: Exits the loop immediately.
  • continue: Skips the remaining code inside the loop for the current iteration and moves to the next iteration.
  • pass: Acts as a placeholder and does nothing. We cannot have empty loops, so we use pass to avoid errors. It can also be used in conditional statements and functions.
# None of these code will actually do anything, but they also won't cause an error.
for i in range(1, 8):
pass

if True:
pass

def unfinsished_function():
pass